(function( $ ){

    var methods = {
		init : function(options) {
		  
		  var settings =   $.extend( {
		      btn_text:'agregar archivo',
		      target_selector:undefined,
		      img_src:undefined,
		      maxwidth:200,
		      maxheight:200,
		      input_name:undefined
		  }, options);
		  
		  create_widget(settings);
		  if(settings.img_src != undefined){
			  load_img(settings);
		  }
		  return this;
	  }
	  	
    };
    
    $.fn.image_uploader = function(method){
    	// Method calling logic
        if ( methods[method] ) {
          return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof method === 'object' || ! method ) {
          return methods.init.apply( this, arguments );
        } else {
          $.error( 'Method ' +  method + ' does not exist on disable' );
        }  
    };
  
  
})( jQuery );

function create_widget(settings){
	var file = $('<input>');
	file.attr('type','file');
	file.attr('name',settings.input_name);
	file.addClass('file');
	file.change(function(){createReader(this.files[0],settings)});
	var main_container = $('<div>');
	main_container.addClass('photo_main_container');
	var photo_container = $('<div>');
	photo_container.addClass('photo_container');
	var button = $('<span>');
	button.attr('id','add_file');
	button.addClass('tiny-button-class');
	button.click(function(){file.click()})
	button.text(settings.btn_text);
	main_container.append(photo_container);
	main_container.append(button);
	file.insertAfter(settings.target_selector);
	main_container.insertAfter(file); 
}

function createReader(file,settings) {
	  var maxwidth = settings.maxwidth;
	  var maxheight = settings.maxheight;
	  var reader = new FileReader();
	  reader.onload = function(evt) {
		  var image = new Image();
		  image.onload = function(evt) {
			  var canvas = document.createElement('canvas');
			  var ctx = canvas.getContext('2d');
			  var ratio = Math.min(maxwidth / this.width, maxheight / this.height, 1);
			  var width = Math.round(this.width * ratio);
			  var height = Math.round(this.height * ratio);
			  var img = null;
			  canvas.width = width;
			  canvas.height = height;
			  ctx.drawImage(image,0,0, width, height);
			  img = document.createElement("img");
			  img.src = canvas.toDataURL(file.type);
			  $(img).addClass('thumbnail');
			  $(img).css('opacity',0);
			  $(".photo_container").html('');
			  $(".photo_container").append(img);
			  $(".photo_container").animate({height:height,width:width},{duration:500, complete:function(){$(img).animate({opacity:1},'fast')} });
		  };
	  
		  image.onerror = function() {
			  message("+= " + file.name + " does not look like a valid image");
		  };
		  
		  image.src = evt.target.result;
	  };
	  
	  reader.readAsDataURL(file);
  }
  
  function load_img(settings){
	  var image = new Image();
	  $(image).css('opacity',0);
	  image.src = settings.img_src;
	  image.onload = function(evt) {
		  var ratio = Math.min(settings.maxwidth / this.width, settings.maxheight / this.height, 1);
		  var width = Math.round(this.width * ratio);
		  var height = Math.round(this.height * ratio);
		  var img = this;
		  $(img).addClass('thumbnail');
		  $(".photo_container").html('');
		  $(".photo_container").append(img);
		  $(".photo_container").animate({height:height,width:width},{duration:500, complete:function(){$(img).animate({opacity:1},'fast')} });
	  };
  }
  
  function center_photo(photo){
	  var photo_container = $(photo).parent();
	// dimensiones de la ventana del explorer 
	   var wscr = $(photo_container).width();
	   var wcnt = $(photo).width() + parseInt($(photo).css('padding-left')) + parseInt($(photo).css('padding-right'));
	// obtener posicion central
	   var mleft = ( wscr - wcnt ) / 2;
	// estableciendo ventana modal en el centro
	   $(photo).css("left", mleft+'px');
	  
  }